﻿using Core.ApplicationServices;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.ApplicationServices
{
    public class Config : IConfigurable
    {
        public int GetNextPage(string key)
        {
            try
            {
                key = key.Replace("-", string.Empty);
                var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var skey = configuration.AppSettings.Settings[key];
                if (skey == null)
                {
                    configuration.AppSettings.Settings.Add(key, "1");
                }
                configuration.Save(ConfigurationSaveMode.Modified);

                return Convert.ToInt32(configuration.AppSettings.Settings[key].Value);
            }
            catch (Exception ex)
            {
                return 1;
            }
        }


        public bool AddNewOrUpdateSetting(string key)
        {
            try
            {
                key = key.Replace("-",string.Empty);
                var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var skey = configuration.AppSettings.Settings[key];
                if (skey == null)
                {
                    configuration.AppSettings.Settings.Add(key, "1");
                }
                else
                {
                    int page = Convert.ToInt32(configuration.AppSettings.Settings[key].Value) + 1;
                    configuration.AppSettings.Settings[key].Value = page.ToString();
                }
                configuration.Save(ConfigurationSaveMode.Modified);

                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public string WorkingFolder
        {
            get
            {
                try
                {
                    var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    return configuration.AppSettings.Settings["PathtoSave"].Value.ToString();
                }
                catch (Exception ex)
                {
                    return Directory.GetCurrentDirectory().Replace("bin" + Path.DirectorySeparatorChar + "Debug", "OutputData");
                }
            }
        }


        public int TimeSyncElapsed
        {
            get
            {
                try
                {
                    var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    return Convert.ToInt32(configuration.AppSettings.Settings["TimeSyncElapsed"].Value.ToString());
                }
                catch (Exception ex)
                {
                    return 30;
                }
            }
        }
    }
}
